package com.ltmonitor.web.controller.video;

import com.ltmonitor.entity.PlatformState;
import com.ltmonitor.entity.SystemConfig;
import com.ltmonitor.service.IPlatformStateService;
import com.ltmonitor.service.ISystemConfigService;
import com.ltmonitor.service.IVideoServerConfigService;
import com.ltmonitor.util.MyFileUtil;
import com.ltmonitor.video.entity.VideoServerConfig;
import com.ltmonitor.web.controller.GenericAction;
import com.ltmonitor.web.util.JsonMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.File;
import java.text.DecimalFormat;

/**
 * 视频平台参数配置
 * 
 * @author DELL
 * 
 */
@Controller
@RequestMapping("/videoServerConfig")
public class VideoServerConfigAction extends GenericAction {

	@Autowired
	protected IPlatformStateService platformStateService;

	@Autowired
	protected IVideoServerConfigService videoServerConfigService;

	protected String EditViewName = "/videoServerConfig/editVideoServerConfig";
	
	@RequestMapping("/view.action")
	public String view(
			Model m) {
		try {
			VideoServerConfig vc = this.videoServerConfigService.getVideoServerConfig();
			if(vc == null)
				vc = new VideoServerConfig();
			m.addAttribute("entity", vc);
			String videoDir = vc.getFtpPath();
			String str = getDataUsage(videoDir);
			m.addAttribute("dataUsage", str);

		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);
			m.addAttribute("message", "发生错误:" + ex.getMessage());
		}
		return "/videoServerConfig/editVideoServerConfig";
	}
	
	/**
	 * 新增后的保存操作
	 * 
	 * @return
	 */
	@RequestMapping("/save")
	public String save(VideoServerConfig d, Model m) throws Exception {

		try {
			//d.setMainDriver(mainDriver);
			//一个车只能有一个主驾驶，需要将同一车的其他主驾驶设置为false
			this.videoServerConfigService.saveOrUpdate(d);

			String videoDir = d.getFtpPath();
			String str = getDataUsage(videoDir);
			m.addAttribute("dataUsage", str);
			m.addAttribute("message", "保存成功");
		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);
			m.addAttribute("message", ex.getMessage());
		}
		m.addAttribute("entity", d);

		return "/videoServerConfig/editVideoServerConfig";
	}

	private String getDataUsage(String videoDir)
	{
		double dataUsage = MyFileUtil.getDirSize(new File(videoDir));
		DecimalFormat df = new DecimalFormat("#.00");
		String str = df.format(dataUsage);
		return str;
	}

	

}
